﻿#define  _CRT_SECURE_NO_WARNINGS 1

//在牛客网学习的第55天

//练习1：多个字符从两端移动，向中间汇聚
//编写代码，演⽰多个字符从两端移动，向中间汇聚

#include<stdio.h>
#include<string.h>


int main()
{
	char arr1[] = "welcome to Beijing";
	char arr2[] = "******************";

	int left = 0;
	int right = strlen(arr1) - 1;

	while (left<=right)
	{
		Sleep(1000);
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		left++;
		right--;
		printf("%s\n", arr2);
	}

	return 0;
}



//在⼀个升序的数组中查找指定的数字n，很容易想到的⽅法就是遍历数组，但是这种⽅法效率⽐较低。
//⽐如我买了⼀双鞋，你好奇问我多少钱，我说不超过300元。你还是好奇，你想知道到底多少，我就让
//你猜，你会怎么猜？你会1，2，3，4...这样猜吗？显然很慢；⼀般你都会猜中间数字，⽐如：150，然
//后看⼤了还是⼩了，这就是⼆分查找，也叫折半查找。

#include<stdio.h>

int main()

{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int k = 0;
	scanf("%d\n", &k);
	int se = sizeof(arr) / sizeof(arr[0]);
	int i = 0;
	for (i = 0; i < se; i++)
	{
		if (arr[i] == k)
		{
			printf("找到了,下标为: %d\n", i);
			break;
		}
	}
	if (i == se)
	{
		printf("找不到了\n");
	}
	return 0;
}




#include<stdio.h>

int main()

{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int k = 0;
	scanf("%zd\n", k);
	int sc = sizeof(arr) / sizeof(arr[0]);
	int left = 0;
	int right = sc - 1;
	while (left<=right)
	{
		int mid = (left + right) / 2;
		if (arr[mid] < k)
			left = mid + 1;
		else if (arr[mid] > k)
			right = mid - 1;
		else
		{
			printf("找到了,下标为: %d\n", mid);
			break;
		}		
    }
	if(left>right)
		printf("找不到了\n");
	return 0;
}




#include <stdio.h>
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]) - 1;
	int key = 7;//要找的数字 
	int mid = 0;//记录中间元素的下标 
	int find = 0;
	while (left <= right)
	{
		mid = (left + right) / 2;
		if (arr[mid] > key)
		{
			right = mid - 1;
		}
		else if (arr[mid] < key)
		{
			left = mid + 1;
		}
		else
		{
			find = 1;
			break;
		}
	}
	if (1 == find)
		printf("找到了,下标是%d\n", mid);
	else
		printf("找不到\n");
}


